package com.is.add.controller.restful;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.is.add.controller.vo.JsonResultVO;
import com.is.add.controller.vo.SettlementVO;
import com.is.add.enmus.OrderStatus;
import com.is.add.enmus.TimeType;
import com.is.add.model.Orders;
import com.is.add.service.SettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 应用到生产表中注意事项：
 * 1.修改结算状态接口 updateSettlementStatus 。业务层方法内注释部分，在完成付款操作时将新结算单合并到原始结算单
 * 2.修改备用金转存接口 preDepositSettle 所使用的表。即修改实体类 表PreDeposit，PreDepositItem
 * 3.userorder 和 userorder_fix 问题。  使用userorder 联合 old_userorder 替代 userorder_fix，或使用历史订单查询方式。
 */
@RestController
@RequestMapping("api/settle")
@CrossOrigin
public class SettleController {
    @Autowired
    private SettleService settleService;

    /**
     * 结算单查询统计--预结算
     */
    @RequestMapping(value = "/findSettleData",method = RequestMethod.GET)
    public List<SettlementVO> findSettleData(@RequestParam(required=false) String keyWord,
                                             @RequestParam(required= false) String startTime,
                                             @RequestParam(required = false) String endTime) throws Exception {
        Map<String,String> map = new HashMap<>();
        if (keyWord != null && !"".equals(keyWord)){ map.put("searchKey",keyWord); }
        if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)){
            startTime = startTime+" 00:00:00";
            endTime = endTime + " 23:59:59";
            map.put("startTime",startTime);
            map.put("endTime",endTime);
        }
        map.put("orderStatus", OrderStatus.VerifiedDateOut.name());
        map.put("timeType", TimeType.examineTime.name());
        map.put("settleStatus","1");
        return this.settleService.loadSettleData(map);
    }


    /**
     * 修复并生成结算单
     */
    @RequestMapping(value = "/createSettlement")
    public JsonResultVO insertNewSettlement(@RequestParam(required=false,defaultValue = "-1") String shopIds,
                                            @RequestParam String startTime,
                                            @RequestParam String endTime){
        return this.settleService.fixAndCheckSettleOrders(shopIds,startTime,endTime);
//        return new JsonResultVO(JsonResultVO.FAILURE,"未到结算周期");
    }

    /**
     * 修改结算状态
     */
    @RequestMapping(value = "/updateSettleStatus",method = RequestMethod.GET)
    public JsonResultVO updateSettlementStatus(@RequestParam String settlementIds,
                                               @RequestParam Integer newStatus){
        return this.settleService.updateSettlementStatus(settlementIds,newStatus);
//        return new JsonResultVO(JsonResultVO.FAILURE,"未到结算周期");
    }

    /**
     * 备用金转存
     */
    @RequestMapping(value = "/preDepositSettle",method = RequestMethod.GET)
    public JsonResultVO preDepositSettle(@RequestParam String settlementIds){
        return this.settleService.insertPreDepositSettleData(settlementIds);
//        return new JsonResultVO(JsonResultVO.FAILURE,"未到结算周期");
    }

    /**
     * 新结算单查询
     */
    @RequestMapping(value = "/findSettleDoc",method = RequestMethod.GET)
    public List<SettlementVO> findSettleDocument(@RequestParam(required=false) String keyWord,
                                             @RequestParam(required= false) String startTime,
                                             @RequestParam(required = false) String endTime,
                                             @RequestParam(required = false,defaultValue = "2") Integer settleStatus) {
        Map<String,Object> map = new HashMap<>();
        if (StringUtil.isNotEmpty(keyWord)){ map.put("keyWord",keyWord); }
        if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)){
            startTime = startTime+" 00:00:00";
            endTime = endTime + " 23:59:59";
            map.put("startTime",startTime);
            map.put("endTime",endTime);
        }
        switch (settleStatus){
            case 5 : map.put("settleStatus",settleStatus); break;   //状态5 查询已付款的结算单
            case 6 : map.put("preDepositStatus",1); break;          //状态6 查询已转存的结算单
            default: map.put("preDepositStatus","0");map.put("settleStatus",settleStatus); break;
        }
        return this.settleService.findSettleDocument(map);
    }

    /**
     * 结算单下的订单
     */
    @RequestMapping(value = "/settleDoc/{settlementId}/Orders",method = RequestMethod.GET)
    public PageInfo<Orders> findOneSettleOrders(@PathVariable String settlementId,
                                             @RequestParam(required = false,defaultValue = "1") int pageNum,
                                             @RequestParam(required = false,defaultValue = "20") int pageSize){
        List<Orders> ordersList = this.settleService.findOneSettleOrders(settlementId,pageNum,pageSize);
        return new PageInfo<>(ordersList);
    }

}
